package com.leo.foodmaster.admin.recipe.service.impl;

import cn.hutool.core.util.StrUtil;
import com.leo.foodmaster.admin.recipe.dto.RecipeComboDTO;
import com.leo.foodmaster.admin.recipe.entity.*;
import com.leo.foodmaster.commons.entity.Result;
import com.leo.foodmaster.commons.util.WebUtils;
import org.springframework.stereotype.Service;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leo.foodmaster.admin.recipe.mapper.RecipeComboMapper;
import com.leo.foodmaster.admin.recipe.service.IRecipeComboService;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 套餐主表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2021-11-09
 */
@Service
@AllArgsConstructor
public class IRecipeComboServiceImpl extends ServiceImpl<RecipeComboMapper, RecipeComboEntity> implements IRecipeComboService {
	private final RecipeComboMapper recipeComboMapper;
	private final IRecipeComboDetailsServiceImpl recipeComboDetailsService;

	/**
	 * 条件分页查询
	 * @param page 分页条件
	 * @param name 名称
	 * @param status 状态
	 * @return IPage<RecipeComboEntity>
	 */
	@Override
	public IPage<RecipeComboEntity> pageList(IPage<RecipeComboEntity> page, String name, String status) {
		Long organId = WebUtils.getUser().getOrganId();
		LambdaQueryWrapper<RecipeComboEntity> qw = Wrappers.lambdaQuery(RecipeComboEntity.class)
				.eq(RecipeComboEntity::getOrganId, organId)
				.like(StrUtil.isNotBlank(name), RecipeComboEntity::getName, name)
				.eq(StrUtil.isNotBlank(status), RecipeComboEntity::getStatus, status)
				.orderByDesc(RecipeComboEntity::getCreateTime);
		IPage<RecipeComboEntity> pageList = recipeComboMapper.selectPage(page, qw);
		List<RecipeComboEntity> records = pageList.getRecords();
		for (RecipeComboEntity record : records) {
			List<RecipeComboDetailsEntity> list = recipeComboDetailsService.list(Wrappers.lambdaQuery(RecipeComboDetailsEntity.class)
					.eq(RecipeComboDetailsEntity::getComboId, record.getId()));
			record.setDetailList(list);
		}
		return pageList;
	}

	/**
	 * 新增套餐
	 * @param dto 套餐信息
	 * @return Boolean
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean addRecipeCombo(RecipeComboDTO dto) {
		Long organId = WebUtils.getUser().getOrganId();
		// 新增菜品模板主表
		RecipeComboEntity recipeComboEntity = new RecipeComboEntity();
		BeanUtil.copyProperties(dto, recipeComboEntity);
		recipeComboEntity.setOrganId(organId);
		save(recipeComboEntity);
		// 新增菜品模板细表
		List<RecipeComboDetailsEntity> detailList = dto.getDetailList();
		detailList.forEach(item ->{
			item.setOrganId(organId);
			item.setComboId(recipeComboEntity.getId());
		});
		return recipeComboDetailsService.saveBatch(detailList);
	}

	/**
	 * 修改套餐
	 * @param dto 套餐信息
	 * @return Boolean
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean updateRecipeCombo(RecipeComboDTO dto) {
		Long organId = WebUtils.getUser().getOrganId();
		// 修改主表信息
		RecipeComboEntity recipeComboEntity = new RecipeComboEntity();
		BeanUtil.copyProperties(dto, recipeComboEntity);
		updateById(recipeComboEntity);

		// 删除原来的细表信息
		recipeComboDetailsService.remove(Wrappers.lambdaUpdate(RecipeComboDetailsEntity.class)
				.eq(RecipeComboDetailsEntity::getComboId, recipeComboEntity.getId()));
		// 新增细表信息
		List<RecipeComboDetailsEntity> detailList = dto.getDetailList();
		detailList.forEach(item ->{
			item.setOrganId(organId);
			item.setComboId(recipeComboEntity.getId());
		});
		return recipeComboDetailsService.saveBatch(detailList);
	}

	/**
	 * 删除套餐信息
	 * @param idList id列表
	 * @return Result<Integer>
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Result<Integer> deleteBatchIds(List<Long> idList) {
		// 删除细表
		for (Long id : idList) {
			recipeComboDetailsService.remove(Wrappers.lambdaUpdate(RecipeComboDetailsEntity.class)
					.eq(RecipeComboDetailsEntity::getComboId, id));
		}
		// 删除主表
		recipeComboMapper.deleteBatchIds(idList);
		return Result.success();
	}
}
